import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { LoginComponent } from './pages/login/login.component';
import { HomeComponent } from './pages/home/home.component';
import { Page1Component } from './pages/page1/page1.component';
import { Page2Component } from './pages/page2/page2.component';
import { Page3Component } from './pages/page3/page3.component';
import { Page4Component } from './pages/page4/page4.component';
import { DynamicComponent } from './pages/dynamic/dynamic.component';
import { LoginGuard } from './guard/loginGuard';
import { DepartureGuard } from './guard/departureGuard';
import { ResolveGuard } from './guard/resolve.Guard';

const routes: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    canActivate: [LoginGuard],
    children: [
      {
        path: 'page1',
        component: Page1Component,
      },
      {
        path: 'page2',
        component: Page2Component,
      },
      {
        path: 'page3',
        component: Page3Component,
      },
      {
        path: 'page4',
        component: Page4Component,
      },

      // 默认路由
      {
        path: '**',
        redirectTo: 'page1',
      },
    ]
  },
  {
    path: 'login',
    component: LoginComponent,
    // canDeactivate: [DepartureGuard],  //处理从当前路由离开的情况。

    // 在路由激活之前获取路由数据
    resolve: {
      product:ResolveGuard,
    }
  },
  {
    path: 'dynamic/:id',
    component: DynamicComponent,
  },
  // 默认跳转路由
  {
    path: '',         //刚进来是空跳转首页
    redirectTo: 'home',
    pathMatch: 'full',
  },
  // 匹配不到路由时候加载的组件
  {
    path: '**',
    redirectTo: 'home',
  },
]

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
  providers: [               //定制的服务
    LoginGuard,              //路由进入时候的守卫
    DepartureGuard,          //离开时候的守卫
    ResolveGuard,            //进入之前的守卫
  ],
})
export class AppRoutingModule { }
